import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from './utils/tool';
import { UploadController } from './upload/upload.controller';
import { PrismaService } from './prisma/prisma.service';
import { UserModule } from './api/user/user.module';
import { LoginModule as AdminLoginModule } from './api/login/login.module';
import { RoleModule as AdminRoleModule } from './api/admin/role/role.module'
import { AuthorityModule as AdminAuthorityModule } from './api/admin/authority/authority.module'
import { ValidateLoginMiddleware } from './validate-login.middleware';

@Module({
  imports: [
    JwtModule.register({
      global: true,
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '7d' },
    }),
    UserModule,
    AdminLoginModule,
    AdminRoleModule,
    AdminAuthorityModule
  ],
  controllers: [AppController, UploadController],
  providers: [AppService, PrismaService],
})
//权限拦截
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(ValidateLoginMiddleware)
      .forRoutes(...["/user", "/admin/*"]); // 使用中间件
  }
}
// export class AppModule { }